package com.creating.mict.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.creating.mict.common.DateUtils;
import com.creating.mict.common.Dom4j;
import com.creating.mict.common.Excel;
import com.creating.mict.common.Files;
import com.creating.mict.common.mess.SMSCaClient;
import com.creating.mict.domain.Contact;
import com.creating.mict.domain.Group;
import com.creating.mict.domain.Patient;
import com.creating.mict.service.IPatientService;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class PatientAction extends ActionSupport {

	@Resource
	private IPatientService patientService;
	
	/**
	 * 
	* @Title: queryPatientSigned 
	* @Description: 获取签约患者
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String queryPatientSigned() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String loginName = (String)request.getSession().getAttribute("loginName");
		String loginOffice = (String)request.getSession().getAttribute("loginOffice");
		String loginType = (String)request.getSession().getAttribute("loginType");
		
		//非超级管理员
		if(!(loginType.equals("1"))) {
			foffice = loginOffice;
		} else {
			loginName = floginname;
		}
		
		List<Patient> list = patientService.queryPatientSigned(loginName, loginType, fbtime, fetime, fstate, fkeywords, foffice); //全部查询，包括隐藏信息
		
		if (list == null) {
			total = 0;
		} else {
			total = list.size();
		}
		
		StringBuffer jsonStr = new StringBuffer("[");
		if (start + limit > total)
			limit = total - start;
		for (int i = start; i < start + limit; i++)
		{
			Patient pa = new Patient();
			pa = list.get(i);
			jsonStr.append("{");
			jsonStr.append("fpid:'").append(pa.getFpid()).append("',");
			jsonStr.append("fbdate:'").append(pa.getFbdateStr()).append("',");
			jsonStr.append("fedate:'").append(pa.getFedateStr()).append("',");
			jsonStr.append("fsign_doctor:'").append(pa.getFsign_doctor()).append("',");
			jsonStr.append("fidcd:'").append(pa.getFidcd()).append("',");
			jsonStr.append("fybkh:'").append(pa.getFybkh()).append("',");
			jsonStr.append("fname:'").append(pa.getFname()).append("',");
			jsonStr.append("fsex:'").append(pa.getFsex()).append("',");
			jsonStr.append("fphone1:'").append(pa.getFphone1()).append("',");
			jsonStr.append("fphone2:'").append(pa.getFphone2()).append("',");
			jsonStr.append("fadd_detail:'").append(pa.getFadd_detail()).append("',");
			jsonStr.append("fxtjc:'").append(pa.getFxyjc()).append("',");
			jsonStr.append("fxyjc:'").append(pa.getFxtjc()).append("',");
			jsonStr.append("fxzjc:'").append(pa.getFxzjc()).append("',");
			jsonStr.append("fxnsjc:'").append(pa.getFxnsjc()).append("',");
			jsonStr.append("fxjgjc:'").append(pa.getFxjgjc()).append("',");
			jsonStr.append("fxybhdjc:'").append(pa.getFxybhdjc()).append("',");
			jsonStr.append("fydjc:'").append(pa.getFydjc()).append("',");
			jsonStr.append("fxdjc:'").append(pa.getFxdjc()).append("',");
			jsonStr.append("fwzfw:'").append(pa.getFwzfw()).append("',");
			jsonStr.append("fstate:'").append(pa.getFstate()).append("',");
			jsonStr.append("fgroup:'").append(pa.getFgroup()).append("',");
			jsonStr.append("foffice:'").append(pa.getFoffice()).append("'");
			
			if (i == (start + limit) - 1)
				jsonStr.append("}");
			else
				jsonStr.append("},");
		}
		jsonStr.append("]");
		String str = "{totalProperty: " + list.size() + ", root:" + jsonStr + "}";
		request.setAttribute("result", str);
		return "success";
	}
	
	
	/**
	 * 
	* @Title: changeSignState 
	* @Description: 解除签约
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String changeSignState() {
		HttpServletRequest request = ServletActionContext.getRequest();
		boolean boo = patientService.changeSignState(fpid, fstate);
		if(boo) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: deleteSignInfo 
	* @Description: 删除签约信息
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String deleteSignInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		boolean boo = patientService.deleteSignInfo(fpid);
		if(boo) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: deleteSignInfo 
	* @Description: 分组
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String groupingPatient() {
		HttpServletRequest request = ServletActionContext.getRequest();
		boolean boo = patientService.groupingPatient(fpid, fgroup);
		if(boo) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: signPatientXyj 
	* @Description: 新增签约 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String savePatientInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		//或当前登录用户名
		String loginName = (String)request.getSession().getAttribute("loginName");
		String loginOffice = (String)request.getSession().getAttribute("loginOffice");
		String loginType = (String)request.getSession().getAttribute("loginType");
		
		Patient pa = new Patient();
		if(fpid == null || fpid.equals("null") || fpid.equals("")) {
			pa.setFpid(DateUtils.getTransactionIdByLongTime());
			pa.setFsign_date(new Date());
		} else {
			pa = patientService.getPatientInfoById(fpid);
		}
		pa.setFsign_doctor(loginName);
		pa.setFidcd(fidcd);
		pa.setFybkh(fybkh);
		pa.setFname(fname);
		pa.setFsex(fsex);
		pa.setFphone1(fphone1);
		pa.setFphone2(fphone2);
		pa.setFadd_detail(fadd_detail);
		
		if(loginType.equals("1")) {
			pa.setFoffice(foffice);
		} else {
			pa.setFoffice(loginOffice);
		}
		
		
		if( fxyjc != null && fxyjc.trim().equals("on")) {
			pa.setFxyjc("1");
		} else {
			pa.setFxyjc("0");
		}
		if( fxtjc != null && fxtjc.trim().equals("on")) {
			pa.setFxtjc("1");
		} else {
			pa.setFxtjc("0");
		}
		if( fxzjc != null && fxzjc.trim().equals("on")) {
			pa.setFxzjc("1");
		} else {
			pa.setFxzjc("0");
		}
		if( fxnsjc != null && fxnsjc.trim().equals("on")) {
			pa.setFxnsjc("1");
		} else {
			pa.setFxnsjc("0");
		}
		if( fxjgjc != null && fxjgjc.trim().equals("on")) {
			pa.setFxjgjc("1");
		} else {
			pa.setFxjgjc("0");
		}
		if( fxybhdjc != null && fxybhdjc.trim().equals("on")) {
			pa.setFxybhdjc("1");
		} else {
			pa.setFxybhdjc("0");
		}
		if( fydjc != null && fydjc.trim().equals("on")) {
			pa.setFydjc("1");
		} else {
			pa.setFydjc("0");
		}
		if( fxdjc != null && fxdjc.trim().equals("on")) {
			pa.setFxdjc("1");
		} else {
			pa.setFxdjc("0");
		}
		if( fwzfw != null && fwzfw.trim().equals("on")) {
			pa.setFwzfw("1");
		} else {
			pa.setFwzfw("0");
		}
		
		pa.setFstate("1");
		pa.setFgroup("999");
		//pa.setFsign_date(new Date());
		pa.setFbdate(DateUtils.parseDate(fbdate));
		pa.setFedate(DateUtils.parseDate(fedate));
		
		//保存签约患者
		boolean boo1 = patientService.savePatientInfo(pa);
		
		if(boo1) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: modPatientXyj 
	* @Description: 修改签约 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String saveContact() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		Contact ca = new Contact();
		ca.setFid(DateUtils.getTransactionIdByLongTime());
		ca.setFidcd(fidcd);
		ca.setFname(fname);
		ca.setFphone(fphone);
		ca.setFtype(ftype);
		
		//保存签约患者联系人
		boolean boo1 = patientService.saveContact(ca);
		
		if(boo1) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: queryContact 
	* @Description: 获取联系人
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String queryContact() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		List<Contact>  list = patientService.queryContact(fidcd);
		
		StringBuffer jsonStr = new StringBuffer("[");
		for (int i = start; i < list.size(); i++)
		{
			Contact ca = new Contact();
			ca = list.get(i);
			jsonStr.append("{");
			jsonStr.append("fid:'").append(ca.getFid()).append("',");
			jsonStr.append("fname:'").append(ca.getFname()).append("',");
			jsonStr.append("ftype:'").append(ca.getFtype()).append("',");
			jsonStr.append("fphone:'").append(ca.getFphone()).append("'");
			
			if (i == list.size() - 1)
				jsonStr.append("}");
			else
				jsonStr.append("},");
		}
		jsonStr.append("]");
		String str = "{totalProperty: " + list.size() + ", root:" + jsonStr + "}";
		request.setAttribute("result", str);
		return "success";
	}
	
	/**
	 * 
	* @Title: deleteContact 
	* @Description: 删除签约患者联系人 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String deleteContact() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		//删除签约患者联系人
		boolean boo1 = patientService.deleteContact(fidcd, fphone);
		
		if(boo1) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: checkQueryKey 
	* @Description: 外网查询验证码
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String checkQueryKey() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String certCode = (String)request.getSession().getAttribute("certCode");
		if(certCode != null && certCode.equals(fkeywords)) {
			request.setAttribute("result", "1"); //OK 
		} else {  
			request.setAttribute("result", "0"); //Error
		}
		return "success";
	}
	
	
	
	
	
	
	/**
	 * 
	* @Title: refreshXyjInfo 
	* @Description: 手动获取监测信息 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String refreshXyjInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		SMSCaClient smscall = new SMSCaClient();
		Dom4j.readXyjReceiveMessXmlAndSave(smscall.testPost(smscall.receiveMessXmlInfo()));
		
		request.setAttribute("result", "OK");
		return "success";
	}
	
	
	/**
	 * 生成excel
	 */
	@SuppressWarnings("rawtypes")
	public String printExcel() {

		// 创建当前日子
		Date date = new Date();
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		// 格式化日期(产生文件名)
		String filename = sdf.format(date);

		// 创建文件
		File f = new File("D://" + filename + ".xls");
		try {
			f.createNewFile();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();

		String headNameStr = request.getParameter("headNameStr"); // 拆分字符串得到表头信息

		String title[] = null;
		if (!"".equals(headNameStr) && null != headNameStr) {
			String str1 = headNameStr.substring(0, headNameStr.length() - 1);
			title = str1.split(",");
		}

		String xhStr = request.getParameter("xhStr"); // 每一行的数据
		String data1[] = null;
		if (!"".equals(xhStr) && null != xhStr) {
			String dataStr1 = xhStr.substring(0, xhStr.length() - 1);
			data1 = dataStr1.split("&");
		}

		List<List> lists = new ArrayList<List>();

		List<String> list3 = new ArrayList<String>();
		list3.add("a");
		list3.add("b");
		list3.add("c");
		for (int i = 0; i < data1.length; i++) {

			String data2[] = null;
			if (!"".equals(data1[i]) && null != data1[i]) {
				String dataStr2 = data1[i].substring(0, data1[i].length() - 1);
				data2 = dataStr2.split(",");
			}

			List<String> list1 = new ArrayList<String>();
			if (data2 != null) {
				for (int j = 0; j < data2.length; j++) {
					list1.add(data2[j]);
				}
			}

			lists.add(list1);
		}

		Excel excels = new Excel();

		// 生成excel文件(保存在服务器机上)
		try {
			excels.writeExcel(new FileOutputStream(f), title, lists);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 导出文件(下载到客户机上,并删除服务器机上的excel文件)
		Files files = new Files();
		try {
			files.exportFile(response, request, f, true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 删除群组
	* @Title: deleteGroup 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String deleteGroupByFid() {
		HttpServletRequest request = ServletActionContext.getRequest();
		boolean boo1 = patientService.deleteGroupByFid(fid);
		if(boo1) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 保存群组
	* @Title: saveGroup 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String saveGroup() {
		HttpServletRequest request = ServletActionContext.getRequest();
		//或当前登录用户名
		String loginName = (String)request.getSession().getAttribute("loginName");
		DateUtils op_date = new DateUtils();
		Group group = new Group();
		if(fid == null || group.equals("null") || group.equals("")) {
			group.setFid(op_date.getTransactionIdByLongTime());
		} else {
			group.setFid(fid);
		}
		group.setFname(fname);
		group.setFstate("1");
		group.setFtype("1");
		group.setFuser(loginName);
		group.setFdesc(fdesc);
		
		boolean boo1 = patientService.saveGroup(group);
		if(boo1) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 获取群组列表
	* @Title: queryGroup 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String queryGroupList() {
		HttpServletRequest request = ServletActionContext.getRequest();
		//或当前登录用户名
		String loginName = (String)request.getSession().getAttribute("loginName");
		List<Group>  list = patientService.queryGroupList(loginName);
		
		StringBuffer jsonStr = new StringBuffer("[");
		for (int i = start; i < list.size(); i++)
		{
			Group group = new Group();
			group = list.get(i);
			jsonStr.append("{");
			jsonStr.append("fid:'").append(group.getFid()).append("',");
			jsonStr.append("fname:'").append(group.getFname()).append("',");
			jsonStr.append("fdesc:'").append(group.getFdesc()).append("',");
			jsonStr.append("fuser:'").append(group.getFuser()).append("'");
			
			if (i == list.size() - 1)
				jsonStr.append("}");
			else
				jsonStr.append("},");
		}
		jsonStr.append("]");
		String str = "{totalProperty: " + list.size() + ", root:" + jsonStr + "}";
		request.setAttribute("result", str);
		return "success";
	}
	
	
	
	private int limit = 500; //分页结束
	private int start = 0;  //分页起始
	private int total;		//总页数
	
	private String fid;
	private String fkeywords;
	private String fbtime;
	private String fetime;
	private String fdesc;
	private String fgroup;
	
	private String query;
	private String fpid;
	private String fname;
	private String fsex;
	private String fidcd;
	private String fadd_detail;
	private String fphone1;
	private String fphone2;
	private String fsign_doctor;
	private String fstate;
	private String ftype;
	private String fybkh; //医保卡号
	
	private String fxyjc;
	private String fxtjc;
	private String fxzjc;
	private String fxnsjc;
	private String fxjgjc;
	private String fxybhdjc;
	private String fydjc;
	private String fxdjc;
	private String fwzfw;
	
	private String fupdate;
	private String foffice;
	private String fbdate;
	private String fedate;
	
	private String fphone;
	private String floginname;

	public String getFloginname() {
		return floginname;
	}
	public void setFloginname(String floginname) {
		this.floginname = floginname;
	}
	public String getFphone() {
		return fphone;
	}
	public void setFphone(String fphone) {
		this.fphone = fphone;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public String getFpid() {
		return fpid;
	}
	public void setFpid(String fpid) {
		this.fpid = fpid;
	}
	public String getFname() {
		return fname;
	}
	public void setFname(String fname) {
		this.fname = fname;
	}
	public String getFsex() {
		return fsex;
	}
	public void setFsex(String fsex) {
		this.fsex = fsex;
	}
	public String getFidcd() {
		return fidcd;
	}
	public void setFidcd(String fidcd) {
		this.fidcd = fidcd;
	}
	public String getFadd_detail() {
		return fadd_detail;
	}
	public void setFadd_detail(String fadd_detail) {
		this.fadd_detail = fadd_detail;
	}
	public String getFphone1() {
		return fphone1;
	}
	public void setFphone1(String fphone1) {
		this.fphone1 = fphone1;
	}
	public String getFphone2() {
		return fphone2;
	}
	public void setFphone2(String fphone2) {
		this.fphone2 = fphone2;
	}
	public String getFsign_doctor() {
		return fsign_doctor;
	}
	public void setFsign_doctor(String fsign_doctor) {
		this.fsign_doctor = fsign_doctor;
	}
	public String getFstate() {
		return fstate;
	}
	public void setFstate(String fstate) {
		this.fstate = fstate;
	}
	public String getFtype() {
		return ftype;
	}
	public void setFtype(String ftype) {
		this.ftype = ftype;
	}
	public String getFybkh() {
		return fybkh;
	}
	public void setFybkh(String fybkh) {
		this.fybkh = fybkh;
	}
	public String getFxyjc() {
		return fxyjc;
	}
	public void setFxyjc(String fxyjc) {
		this.fxyjc = fxyjc;
	}
	public String getFxtjc() {
		return fxtjc;
	}
	public void setFxtjc(String fxtjc) {
		this.fxtjc = fxtjc;
	}
	public String getFxzjc() {
		return fxzjc;
	}
	public void setFxzjc(String fxzjc) {
		this.fxzjc = fxzjc;
	}
	public String getFxnsjc() {
		return fxnsjc;
	}
	public void setFxnsjc(String fxnsjc) {
		this.fxnsjc = fxnsjc;
	}
	public String getFxjgjc() {
		return fxjgjc;
	}
	public void setFxjgjc(String fxjgjc) {
		this.fxjgjc = fxjgjc;
	}
	public String getFxybhdjc() {
		return fxybhdjc;
	}
	public void setFxybhdjc(String fxybhdjc) {
		this.fxybhdjc = fxybhdjc;
	}
	public String getFydjc() {
		return fydjc;
	}
	public void setFydjc(String fydjc) {
		this.fydjc = fydjc;
	}
	public String getFxdjc() {
		return fxdjc;
	}
	public void setFxdjc(String fxdjc) {
		this.fxdjc = fxdjc;
	}
	public String getFwzfw() {
		return fwzfw;
	}
	public void setFwzfw(String fwzfw) {
		this.fwzfw = fwzfw;
	}
	public String getFupdate() {
		return fupdate;
	}
	public void setFupdate(String fupdate) {
		this.fupdate = fupdate;
	}
	public String getFoffice() {
		return foffice;
	}
	public void setFoffice(String foffice) {
		this.foffice = foffice;
	}
	public String getFbdate() {
		return fbdate;
	}
	public void setFbdate(String fbdate) {
		this.fbdate = fbdate;
	}
	public String getFedate() {
		return fedate;
	}
	public void setFedate(String fedate) {
		this.fedate = fedate;
	}
	public String getFkeywords() {
		return fkeywords;
	}
	public void setFkeywords(String fkeywords) {
		this.fkeywords = fkeywords;
	}
	public String getFbtime() {
		return fbtime;
	}
	public void setFbtime(String fbtime) {
		this.fbtime = fbtime;
	}
	public String getFetime() {
		return fetime;
	}
	public void setFetime(String fetime) {
		this.fetime = fetime;
	}


	public String getFid() {
		return fid;
	}


	public void setFid(String fid) {
		this.fid = fid;
	}


	public String getFdesc() {
		return fdesc;
	}


	public void setFdesc(String fdesc) {
		this.fdesc = fdesc;
	}


	public String getFgroup() {
		return fgroup;
	}


	public void setFgroup(String fgroup) {
		this.fgroup = fgroup;
	}
}
